# This Makefile ==============================================================
#	make			-- compile all
#						Steps:
#						1) Compile and link $(OBJECTS) (.s -> .bin)
#						2) Convert BIN to Intel HEX, ($PRJ_NAME).hex
#	make clean		-- clear all object and executed files
#=============================================================================

# Project name ===============================================================
PRJ_NAME := bl_j128gp306

# Directories ----------------------------------------------------------------
PIC_INC := /usr/pic30-elf/support/inc
PIC_GLD := /usr/pic30-elf/support/gld
PIC_LIB := /usr/pic30-elf/lib
APP := .

# Console Commands ------------------------------------------------------------
CC := pic30-elf-gcc
B2H	:= pic30-elf-bin2hex

# Flags options ---------------------------------------------------------------
# -O0:		no optimisation of code
# -I:		include the directory of header file (*.inc)
# -T:		include the linker script file (*.gld)
# -Xlinker:	pass argument from pic30-elf-gcc (compiler) to pic30-elf-ld (linker)
#			e.g. 	-Map file (create a map file)
#					--print-map (print a map file on screen)
#					--report-mem (print the memory usage summary on screen)
#					[for more arguments, refer to: 
#					MPLAB ASM30 MALAB LINK30 and utilities user's guide]
CFLAG := -O0 -Xlinker -Map="$(APP)/$(PRJ_NAME).map" \
			-I$(PIC_INC)/ -T$(PIC_GLD)/p33FJ128GP306.gld

#			-Xlinker --no-data-init \

# Target file -----------------------------------------------------------------
TARGET := $(PRJ_NAME).bin
TARGET_HEX := $(PRJ_NAME).hex

# Object files ----------------------------------------------------------------
OBJECTS := $(APP)/bl_j128gp306.s

#==============================================================================

# Compile and linking =========================================================

# STEP 2 ----------------------------------------------------------------------
# This first explicit rule in the file, which is the rule that make
# will attempt to build when you type "make".  In this case, "all" is 
# a dummy parameter for the final *.hex, which is converted by $(B2H)
# from $(TARGT). Before make can do "nothing", it must build $(TARGT).
all: $(TARGET)
	@echo 'Building target: $(TARGET_HEX)'
	@echo 'Invoking: pic30-elf-bin2hex'
	$(B2H) $^ $(TARGET_HEX)
	@echo 'Finished building target: $(TARGET_HEX)'
	@echo ' '
	
# STEP 2 ----------------------------------------------------------------------
# This first explicit rule in the file, which is the rule that make
# will attempt to build when you type "make".  In this case, "all" is 
# a dummy parameter for the final *.hex, which is converted by $(B2H)
# from $(TARGT). Before make can do "nothing", it must build $(TARGT).
$(TARGET): $(OBJECTS)
	@echo 'Building target: $@'
	@echo 'Invoking: pic30-elf-gcc'
	$(CC) $(CFLAG) $^ -o $@
	@echo 'Finished building target: $@'
	@echo ' '
	
# This final rule is what's called a "phony rule" or "phony target",
# because it isn't used in ary part of the normal build process.
# By typing "make clean", make will attempt to build this rule and
# these commands will delete all of the compiler generated files.
clean:
	find $(APP)/ \( -name '*.bin' -o -name '*.hex' -o -name '*.map' -o -name '*.o' \) -print -exec rm -f '{}' ';'

